<!--页面主要内容-->
<main class="lyear-layout-content">

  <div class="container-fluid p-t-15">

    <div class="row">
      <div class="col-lg-12">
        <div class="card">
          <div class="card-body">

            <form method="post" class="row" enctype="multipart/form-data">

              <div class="form-group col-md-12">
                <label for="name">商品名称</label>
                <input type="text" class="form-control" id="name" name="name" value="{$pro.name}" placeholder="请输入标题" />
              </div>

              <div class="form-group col-md-12">
                <label for="cate">商品分类</label>
                <div class="form-controls">
                  <select name="cate" class="form-control" id="cate">
                    {foreach $type as $item}
                    <option value="{$item.id}" {$pro.typeid == $item.id ? 'selected' : ''}>{$item.name}</option>
                    {/foreach}
                  </select>
                </div>
              </div>

              <div class="form-group col-md-12">
                <label for="price">商品价格</label>
                <input type="text" class="form-control" id="price" name="price" value="{$pro.price}" placeholder="请输入商品介个" />
              </div>

              <div class="form-group col-md-12">
                <label for="stock">商品库存</label>
                <input type="text" class="form-control" id="stock" name="stock" value="{$pro.stock}" placeholder="请输入商品库存" />
              </div>

              <div class="form-group col-md-12">
                <label for="flag">销售状态</label>
                <div class="form-controls">
                  <select name="flag" class="form-control" id="flag">
                    <option value="1" {$pro.flag == 1 ? 'selected' : ''}>新品</option>
                    <option value="2" {$pro.flag == 2 ? 'selected' : ''}>热销</option>
                    <option value="3" {$pro.flag == 3 ? 'selected' : ''}>推荐</option>
                  </select>
                </div>
              </div>

              <div class="form-group col-md-12">
                <label>图片上传</label>
                <div class="input-group" style="display: block;">
                  <label for="thumbs" onclick="upload('#thumbs','.img')" style="cursor: pointer;">
                    <input type="file" class="form-control" name="thumbs" id="thumbs" hidden>
                    <img class="img" src="{$pro.cover_cdn ? $pro.cover_cdn : '/static/home/res/images/upload.png'}" id="img-thumbs" width="200">
                  </label>
                </div>
              </div>
              <div class="form-group col-md-12">
                <button type="" class="btn btn-primary" id="onSubmit">确 定</button>
                <a type="button" class="btn btn-default" href="{:url('admin/Product/list')}">返
                  回</a>
              </div>
            </form>

          </div>
        </div>
      </div>

    </div>

  </div>

</main>
<!--End 页面主要内容-->

<script>

  // layui.use(['layer'], function () {
  //   var $ = layui.jquery,
  //     layer = layui.layer

  //   $('#onSubmit').click(function () {
  //     $.ajax({
  //       type: 'post',
  //       url: `{:url('admin/product/listAdd')}`,
  //       dataType: 'json',
  //       success: function (res) {
  //         console.log(res)
  //         if (res.code == 1) {
  //           layer.msg(res.msg, { icon: 1, time: 1500 }, function (index) {
  //             // location.href = res.url
  //             // location.reload()
  //             layer.close(index)
  //           })
  //         } else {
  //           layer.msg(res.msg, { icon: 2, time: 1500 }, function (index) {
  //             layer.close(index)
  //             // location.reload()
  //           })
  //         }
  //       }
  //     })
  //   })


  // })

  // 图片预览函数
  function GetObjectURL (file) {
    // createObjectURL  给一个文件对象 可以提取出一个url本地地址出来
    var url = null;

    //判断是否为IE浏览器还是google还是其他浏览器
    if (window.createObjectURL != undefined) {
      url = window.createObjectURL(file)
    } else if (window.URL != undefined) {
      url = window.URL.createObjectURL(file)
    } else if (window.webkitURL != undefined) {
      url = window.webkitURL.createObjectURL(file)
    }

    return url
  }

  function upload (elem, img) {
    // 点击拉起上传选择
    $(`${elem}`).click(function (param) {
    })

    $(elem).change(function () {
      let file = $(this)[0].files[0] ? $(this)[0].files[0] : {}
      let id = $(this).attr('id')
      let url = GetObjectURL(file)
      let imgArr = $(img);
      for (let item of imgArr) {
        let itemid = $(item).attr('id')
        if ('img-' + id == itemid) {
          $(item).attr('src', url)
        }
      }
    })
  }

</script>